package best.driver.android.View;

import android.content.Context;
import android.text.*;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import best.driver.android.R;

public class CanClearEditText extends RelativeLayout {

    private View viewClear;
    private EditText editText;
    private boolean focus = false;

    private String defaultText = "";
    private String hintText;
    private Boolean singleLine = true;
    private int inputType;
    private Boolean  isForbidSpace;//是否过滤空格

    public CanClearEditTextListener getTextListener() {
        return textListener;
    }

    public void setTextListener(CanClearEditTextListener textListener) {
        this.textListener = textListener;
    }

    private CanClearEditTextListener textListener;

    public CanClearEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttributes(context, attrs);
        initView();
    }

    public Boolean getIsForbidSpace() {
        return isForbidSpace;
    }

    //设置是否过滤空格
    public void setIsForbidSpace(Boolean isForbidSpace) {
        this.isForbidSpace = isForbidSpace;
        if (isForbidSpace){
             editText.setFilters( new  InputFilter[] {
                     spacefilter
             });
        }
    }

    //限制只输入数字
    public void setIsOnlyNumber(Boolean isOnlyNumber) {
        if (isOnlyNumber){
            editText.setFilters( new  InputFilter[] {
                    numberonlyFilter
            });
        }
    }

    public void setMaxLength(int length) {
        editText.setFilters(new  InputFilter[]{ new  InputFilter.LengthFilter(18)});
    }
    //限制输入数字和最大长度
    public void setIsOnlyNumber(Boolean isOnlyNumber,int maxlength) {
        if (isOnlyNumber){
            editText.setFilters( new  InputFilter[] {
                    numberonlyFilter,new InputFilter.LengthFilter(maxlength)
            });
        }
    }

    InputFilter spacefilter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {

            for (int i = start; i < end; i++) {
                if (Character.isSpaceChar(source.charAt(i))) {
                    return "";
                }
            }
            return null;
        }
    };


    InputFilter numberonlyFilter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {

            for (int i = start; i < end; i++) {
                if (!Character.isDigit(source.charAt(i))) {
                    return "";
                }
            }
            return null;
        }
    };




    private void initAttributes(Context context, AttributeSet attrs) {

        int textResource = attrs.getAttributeResourceValue(null, "text", 0);
        if (textResource != 0) {
            defaultText = context.getString(textResource);
        } else {
            defaultText = attrs.getAttributeValue(null, "text");
        }

        int hintResource = attrs.getAttributeResourceValue(null, "hint", 0);
        if (hintResource != 0) {
            hintText = context.getString(hintResource);
        } else {
            hintText = attrs.getAttributeValue(null, "hint");
        }

        singleLine = attrs.getAttributeBooleanValue(null, "singleLine", true);


        String strInputType = attrs.getAttributeValue(null, "inputType");
        inputType = getInputType(strInputType);
    }

    private int getInputType(String strInputType) {
        if ("number".equals(strInputType)) {
            return InputType.TYPE_CLASS_NUMBER;
        } else {
            return InputType.TYPE_CLASS_TEXT;
        }
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_can_clear_edit_text, this);
        viewClear = findViewById(R.id.can_clear_edit_text_btn_clear);
        editText = (EditText) findViewById(R.id.can_clear_edit_text);

        if (!TextUtils.isEmpty(defaultText)) {
            editText.setText(defaultText);
        }

        if (!TextUtils.isEmpty(hintText)) {
            editText.setHint(hintText);
        }

        editText.setInputType(inputType);

        editText.setSingleLine(singleLine);

        editText.setTextColor(getResources().getColor(R.color.c333));
        editText.setHintTextColor(getResources().getColor(R.color.ccc));

        editText.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View arg0, boolean arg1) {
                focus = arg1;
                checkText();
            }
        });
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub
                if (textListener != null){
                    textListener.onTextChanged(arg0,arg1,arg2,arg3);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub
                if (textListener != null){
                    textListener.beforeTextChanged(arg0,arg1,arg2,arg3);
                }
            }

            @Override
            public void afterTextChanged(Editable arg0) {
                if (textListener != null){
                    textListener.afterTextChanged(arg0);
                }
                checkText();
            }
        });
        viewClear.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                editText.setText("");
            }
        });
    }

    private void checkText() {
        if (focus) {
            boolean hasContent = !TextUtils.isEmpty(editText.getText().toString());
            viewClear.setVisibility(hasContent ? View.VISIBLE : View.GONE);
        } else {
            viewClear.setVisibility(View.GONE);
        }
    }

    public EditText getEditText() {
        return editText;
    }

    public void setText(String str) {
        editText.setText(str);
    }

    public String getText() {
        return editText.getText().toString();
    }

    public void addTextChangedListener(TextWatcher watcher) {
        editText.addTextChangedListener(watcher);
    }

    public void setHint(int resid) {
        editText.setHint(resid);
    }

    public void setHint(String strHint) {
        editText.setHint(strHint);
    }

    public void setSingleLine(boolean singleLine) {
        editText.setSingleLine(singleLine);
    }

    public void setInputType(int type) {
        editText.setInputType(type);
    }
}
